package com.ssm.pre.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.dao.OptionDao;
import com.ssm.pre.service.OptionService;
import com.ssm.pre.util.PageCount;
import com.ssm.pre.util.TemplateUtil;
import com.ssm.pre.util.ToolUtil;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;

@Service
public class OptionServiceImpl implements OptionService {

	@Autowired
	private OptionDao optionDao;

	/**
	 * 增加一条意见反馈
	 */
	@Override
	public void addOption(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		map.put("createTime", ToolUtil.getTimeAndToString());
		optionDao.addOption(map);
		//发送意见反馈的模板消息
		TemplateUtil.Template2(map);
	}

	/**
	 * 根据ID删除一条记录
	 */
	@Override
	public void deleteOptionById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		if(map != null){
			optionDao.deleteOptionById(map);
		}else{
			outputObject.setreturnMessage("错误参数.");
		}
	}

	/*
	 * 查询所有的意见反馈记录,先用itcast_user的用户名称代替
	 */
	@Override
	public void queryOptionByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = optionDao.queryOptionByList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size()); 
	}
	
	
}
